package luanvan.app.foodnote;

import luanvan.app.foodnote.library.ConnectionDetector;
import luanvan.app.foodnote.library.UserFunctions;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
	Button btnPlaces;
	Button btnSearch;
	Button btnReport;
	Button btnHelp;
	Button btnAbout;

	UserFunctions userFunction;
	
	// flag for Internet connection status
    Boolean isInternetPresent = false;
 
    // Connection detector class
    ConnectionDetector cd;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Check login status in database
		userFunction = new UserFunctions();
		if(userFunction.isUserLoggedIn(getApplicationContext())){
			setContentView(R.layout.activity_main);
			
			btnPlaces = (Button) findViewById(R.id.btnPlaces);
			btnSearch = (Button)findViewById(R.id.btnSearch);
			btnHelp = (Button) findViewById(R.id.btnHelp);
			btnAbout = (Button) findViewById(R.id.btnAbout);
			
			cd = new ConnectionDetector(getApplicationContext());
			isInternetPresent = cd.isConnectingToInternet();
			
			
			OnClickListener places = new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent a = new Intent(MainActivity.this, AllFoodsActivity.class);
					startActivity(a);
				}
			};
			
			OnClickListener search = new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent a = new Intent(MainActivity.this,FoodSearchActivity.class);
					startActivity(a);
				}
			};
			
			
			OnClickListener help = new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent b = new Intent(MainActivity.this, HelpActivity.class);
					startActivity(b);
				}
			};
			OnClickListener about = new OnClickListener() {

				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent b = new Intent(MainActivity.this, AboutFoodActivity.class);
					startActivity(b);
				}
			};
			btnPlaces.setOnClickListener(places);
			btnSearch.setOnClickListener(search);
			btnHelp.setOnClickListener(help);
			btnAbout.setOnClickListener(about);
		}
		else {
			Intent login = new Intent(getApplicationContext(), LoginActivity.class);
			login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(login);
            // Closing dashboard screen
            finish();
		}
		
	}

	@Override
	public void onDestroy(){
		super.onDestroy();
		userFunction.logoutUser(getApplicationContext());
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item){
		/*if(item.getItemId()==R.id.exit){
			finish();
			System.exit(0);
		}*/
		return (super.onOptionsItemSelected(item));
		
	}
}
